﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Gma.QrCodeNet.Encoding;
using System.Text.RegularExpressions;


namespace ex_1
{
    class Program
    {
        static void Main(string[] args)
        {

            Console.BackgroundColor = ConsoleColor.White;
            Console.ForegroundColor = ConsoleColor.Black;           //Change the color.

            Console.Write(@"Type some text to QR code: ");
            string sampleText = Console.ReadLine();                 //Input sample text.

            if (sampleText.Length != 0)                             //The string's length can't be 0.
            {
                Try try1 = new Try();
                try1.Again(sampleText);                             //Create the QRCode.
            }

            Console.WriteLine(@"Press any key to quit.");
            Console.ReadKey();                                      //May be like return 0.
        }

    }


    public class Try
    {
        public void Again(string str)
        {
            bool boo = new Regex(@"^[A-Za-z0-9]+$").Match(str).Success;             //regular expression to explain only can input letters and numbers

            if (boo == true)
            {
                QrEncoder qrEncoder = new QrEncoder(ErrorCorrectionLevel.M);        //Set up the fault-tolerant rate to M level--15%.
                QrCode qrCode = qrEncoder.Encode(str);                              //Send the text information to QR.
                for (int j = 0; j < qrCode.Matrix.Width; j++)                       //Coding and translating the matrix to pixel array.
                {
                    for (int i = 0; i < qrCode.Matrix.Width; i++)
                    {
                        char charToPrint = qrCode.Matrix[i, j] ? '█' : '　';         
                        Console.Write(charToPrint);
                    }
                    Console.WriteLine();
                }
            }

            else
            {
                Console.WriteLine(@"Please try again.");
                Console.Write(@"Type some text to QR code: ");
                string sampleText = Console.ReadLine();
                Again(sampleText);                                                  //Recurrence until create the Qrcode successfully.
            }   
            
        }

    }
    
}